package edu.neu.javaweb.dao;

import edu.neu.javaweb.bean.User;
import edu.neu.javaweb.utils.DBUtil;
import io.github.biezhi.anima.Anima;

import io.github.biezhi.anima.Anima;

import static io.github.biezhi.anima.Anima.update;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
//        static DBUtil dbUtil = DBUtil.getInstance();
//    static Anima anima = Anima.open("jdbc:mysql://146.56.213.198:3306/hr?characterEncoding=utf-8&serverTimezone=GMT%2B8", "test", "test");
//    static Anima anima = DBUtil.getInstance().getAnima();
//    public User getUserByNameAndPwd1(String username, String passwd) {
//        try {
//            String sql = "select * from users where username = ? and password = ?";
//
//            ResultSet rs = dbUtil.query(sql, username, passwd);
//            if (rs == null) {
//                return null;
//            }
//            List<User> userList = new ArrayList<>();
//            while (rs.next()) {
//                userList.add(new User(
//                        rs.getString(1),
//                        rs.getString(2),
//                        rs.getString(3),
//                        rs.getString(4),
//                        rs.getString(5)
//                ));
//            }
//            return userList.size() == 1 ? userList.get(0) : null;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public User getUserByNameAndPwd(String username, String passwd) {
        String sql = "select * from users_zh where username = ? and password = ?";
        List<User> userList = Anima.select().bySQL(User.class, sql, username, passwd).all();
        return userList.size() == 0 ? null : userList.get(0);
    }

    public boolean addUser(User u) {
        String sql = "insert into users_zh values(?, ?, ?, ?)";
        int re = Anima.execute(sql, u.getUserName(), u.getPassWord(), u.getSex(), u.getCollege());
        if (re == 1) {
            return true;
        }
        return false;
    }

    public List<User> findUser(String username) {
        String sql = "select * from users_zh where username = ?";
        List<User> userList = Anima.select().bySQL(User.class,sql,username).all();
        return userList;
    }
}
